/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Kasir;

import Entity.Barang;
import Manager.TestConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Axioo
 */
public class transaksiController {

    private static Connection konek = new TestConnection().getConnection();

    public transaksiController() {
    }

    public static Barang getDataBarang(String id) throws SQLException {
        Barang barang = new Barang();
        String sql = "select NAMABARANG, HARGABARANG, STOK from BARANG where KODEBARANG = ?";
        PreparedStatement ps = konek.prepareStatement(sql);
        ps.setString(1, id);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            barang.setNamabarang(rs.getString(1));
            barang.setHargabarang(rs.getDouble(2));
            barang.setStock(rs.getInt(3));
            return barang;
        } else {
            return null;
        }
    }

    public static String getIDTransaksi() {
        try {
            String sql = "select IDTRANSAKSI from TRANSAKSI order by IDTRANSAKSI desc";
            PreparedStatement ps = konek.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int a = Integer.parseInt(rs.getString(1).substring(3)) + 1;
                String nota = "" + a;
                while (nota.length() != 3) {
                    nota = "0" + nota;
                }
                return "ME-"+nota;
            } else {
                return "ME-000";
            }
        } catch (SQLException ex) {
            return "0000";
        }

    }
}
